package com.th.contraller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.th.dao.AdmDao;
import com.th.dao.UserDao;
import com.th.dao.UserDaoImp;
import com.th.entity.Adm;
import com.th.entity.User;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name="userLogin",urlPatterns = {"/userLogin"})
public class UserLogin extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        PrintWriter out = res.getWriter();
        String js_code = req.getParameter("js_code");
        String openid = getOpenid(js_code);
        if(openid.startsWith("{\"err"))
        {
            out.write(openid);
            return;
        }
        ObjectMapper mapper = new ObjectMapper();
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        if(openid.equals("oTBgA7Qx-gOoVXq6Pkgztp2QI2To"))
        {
            AdmDao admDao = (AdmDao) context.getBean("admDaoImp");
            Adm adm = admDao.selectAdm(openid);
            req.getSession().setAttribute("adm",adm);
            out.write(mapper.writeValueAsString(adm));
        }
        else
        {
            UserDao userDao = (UserDao) context.getBean("userDaoImp");
            User user = userDao.selectUser(openid);
            if(user != null)
            {
                req.getSession().setAttribute("user",user);
                out.write(mapper.writeValueAsString(user));
            }
            else
            {
                user = new User();
                user.setOpenid(openid);
                req.getSession().setAttribute("user",user);
                out.write(mapper.writeValueAsString(user));
                ((UserDao) context.getBean("userDaoImp")).insertUser(user);
            }
        }
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {

    }
    //获取用户openid
    public String getOpenid(String js_code) throws IOException {
        RestTemplate restTemplate = new RestTemplateBuilder().build();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx4169b8828ed1c277&secret=d48eee9b6643c294d732d74c339946db&js_code="+js_code+"&grant_type=authorization_code";
        String response = restTemplate.getForObject(url, String.class);
        String result = response;
        System.out.println(result);
        if(result.split(",")[0].split(":")[0].startsWith("{\"err"))
        {
            return result;
        }
        String openid = result.split(",")[1].split(":")[1].replaceAll("\"}*","");
        return openid;
    }
}
